<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card
        v-loading="loading"
        class="tree-card"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <!-- 头部组件-->
        <TreeTools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!-- 组织架构的树形组件 -->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!-- 作用域插槽 -->
          <template #default="{data}">
            <TreeTools :tree-node="data" @del-dept="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
          </template>
        </el-tree>
      </el-card>
    </div>

    <!-- 弹窗组件 -->
    <AddDept ref="addDept" :show-dialog.sync="showAddDialog" :tree-node="node" @addDeptS="getDepartments" />
  </div>
</template>

<script>
import AddDept from './components/add-dept.vue' // 增加部门的弹窗组件
import TreeTools from './components/tree-tools.vue'
import { reqgetDepartments } from '@/api/departments' // 获取部门列表的接口函数
import { tranListTotreeData } from '@/utils/index' // 递归方法
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      defaultProps: {
        label: 'name'
      },
      departs: [],
      company: {},
      showAddDialog: false, // 控制新增部门的弹窗显示
      node: null, // 获取当前新增部门的信息
      // 加载圈
      loading: false
    }
  },
  created() {
    this.getDepartments() // 获取组织架构的方法
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const result = await reqgetDepartments()
      this.company = { name: result.companyName, manager: '负责人' }
      // 把对象数组转换成树形数组
      this.departs = tranListTotreeData(result.depts, '')
      this.loading = false
    },
    addDepts(node) { // 新增部门事件
      this.node = node // 因为node是当前的点击的部门， 此时这个部门应该记录下来,
      this.showAddDialog = true // 显示弹层
    },
    editDepts(node) { // 编辑部门事件
      this.node = node // 因为node是当前的点击的部门， 此时这个部门应该记录下来,
      this.showAddDialog = true // 显示弹层
      // 获取组件实例
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>
<style>
</style>

<style lang="scss" scoped>
.el-tree {
  // 深度穿透选择器
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
